package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.itheima.dao.MemberDao;
import com.itheima.pojo.Member;
import com.itheima.service.MemberService;
import com.itheima.utils.MD5Utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @PackageName: com.itheima.service.impl
 * @ClassName: MemberServiceImpl
 * @Author: LiuJunMeng
 * @Date: 2020/2/7 10:33
 * @Description: //TODO
 */
@Service(interfaceClass = MemberService.class)
@Transactional
public class MemberServiceImpl implements MemberService {
    @Autowired
    private MemberDao memberDao;

    @Override
    public Member findByTelephone(String telephone) {
        return memberDao.findByTelephone(telephone);
    }

    @Override
    public void add(Member member) {
        String password = member.getPassword();
        if (password != null) {
            password = MD5Utils.md5(password);
            member.setPassword(password);
        }
        memberDao.add(member);
    }

    /**
     * 会员数量统计
     *
     * @param month
     * @return
     */
    @Override
    public List<Integer> findMemberCountByMonth(List<String> month) {
        List<Integer> list = new ArrayList<>();
        if (month != null && month.size() > 0) {
            for (String m : month) {
                m = m + "-31";
                Integer count = memberDao.findMemberCountBeforeDate(m);
                list.add(count);
            }
        }
        return list;
    }

    /**
     *会员数据统计按天数
     *@returns
     */
    public List<Integer> findMemberCountBydate(List<String> dateBetween) {
        List<Integer> list = new ArrayList<>();
        if (dateBetween != null && dateBetween.size() > 0) {
            for (String s : dateBetween) {
                Integer count = memberDao.findMemberCountBeforeDate(s);
                list.add(count);
            }
        }
        return list;
    }

    ////马志强代码开始
    @Override
    public List<Map> getAllBySex() {
        return memberDao.getAllBySex();
    }

    @Override
    public List<Map> getAllByAge() {
        return memberDao.getAllByAge();
    }
    //马志强代码over
}
